package life.sweet.community.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class PaginationDTO<T> {
    private List<T> data;
    private boolean showPrevious;
    private boolean showFirstPage;
    private boolean showNextPage;
    private boolean showEndPage;
    private Integer totalPage;

    private Integer page;
    private List<Integer> pages = new ArrayList<>();

    /**
     * 分页设置
     * @param totalPage 总页数
     * @param page 当前页
     * @param size 每页条数
     */
    public void setPagination(Integer totalPage, Integer page, Integer size) {
        this.totalPage = totalPage;
        this.page = page;
        // pages
        pages.add(page);
        for (int i = 1; i <= 3 ; i++) {
            if(page - i > 0) {
                pages.add(0,page - i);
            }
            if(page + i <= totalPage) {
                pages.add(page + i);
            }
        }

        // 是否显示首页
        showFirstPage = !pages.contains(1);
        // 是否显示末页
        showEndPage = !pages.contains(totalPage);
        // 上一页
        showPrevious = !(page == 1);
        // 下一页
        showNextPage = !(page == totalPage);
    }
}
